package com.demo.webservicedemo.controller;


import com.demo.webservicedemo.entity.GMItemCreate;
import com.demo.webservicedemo.entity.InboundOrderRequest;
import com.demo.webservicedemo.entity.InboundOrderResponse;
import com.demo.webservicedemo.entity.MBGMCR04;
import com.demo.webservicedemo.service.Impl.MaterialEntryServiceImpl;
import com.demo.webservicedemo.utils.XmlUtils;
import jakarta.xml.bind.JAXBException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WebServiceController {

    @Autowired
    private MaterialEntryServiceImpl materialEntryServiceImpl;

    //访问的url
    @RequestMapping(value="/wsrest",  method = RequestMethod.POST)
    public InboundOrderResponse processInboundOrder(@RequestBody String requestStr) throws JAXBException {
        MBGMCR04 request = (MBGMCR04) XmlUtils.xmlToObject(requestStr,MBGMCR04.class);

        if (request == null ) {
            throw new IllegalArgumentException("Invalid request structure");
        }
        MBGMCR04 mbgmcr04 = request;
        // 处理业务逻辑
        System.out.println("Received order for material: " +
                mbgmcr04.getIdoc());
        GMItemCreate gmItemCreate = mbgmcr04.getIdoc().getE1mbgmcr().getGmItemCreate();
        int result = materialEntryServiceImpl.createEntry(gmItemCreate);
        // 创建响应
        InboundOrderResponse response = new InboundOrderResponse();
        if (result == 1) {
            response.setStatus("SUCCESS");
            response.setMessage("Order processed successfully");
            response.setDocumentNumber("SUCCESS");
        }else {
            response.setStatus("FAILURE");
            response.setMessage("Order processed failure");
            response.setDocumentNumber("FAILURE");
        }
        return response;
    }

}
